﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WCFService;
using Database;

namespace DataAccessLayer
{
    public class PostData
    {
        public int ForumID { get; set; }
        public int SubForumID { get; set; }
        public int ThreadID { get; set; }
        public int ID { get; set; }
        public String Title { get; set; }
        public String body { get; set; }
        public String Creator { get; set; }
        public String Time { get; set; }
    }

    public class ThreadDataLayer
    {
        public static List<PostData> GetThreadData(int forumID, int subForumID, int threadID)
        {
            List<PostData> pdList = new List<PostData>();

            AppDomain.CurrentDomain.SetData("SQLServerCompactEditionUnderWebHosting", true);
            LogicForumImp forumManager = new LogicForumImp();
            Thread threadDataObj = forumManager.GetWebThread(forumID, subForumID, threadID);

            SortedDictionary<int, Post> posts = threadDataObj.getPosts();

            foreach (Post p in posts.Values)
            {
                pdList.Add(new PostData { ForumID = p.getForumId(), SubForumID = p.getSubForumId(), ThreadID = p.getThreadId(), ID = p.getID(), Title = p.getTitle(), body = p.getBody(), Creator = p.getUser().getUserName(), Time = p.getCreationTime().ToString() });
            }

            return pdList;
        }

        public static String GetTitle(int forumID, int subForumID, int threadID)
        {
            AppDomain.CurrentDomain.SetData("SQLServerCompactEditionUnderWebHosting", true);
            LogicForumImp forumManager = new LogicForumImp();
            return forumManager.GetWebThread(forumID, subForumID, threadID).getTitle();
        }

        public static String GetBody(int forumID, int subForumID, int threadID)
        {
            AppDomain.CurrentDomain.SetData("SQLServerCompactEditionUnderWebHosting", true);
            LogicForumImp forumManager = new LogicForumImp();
            return forumManager.GetWebThread(forumID, subForumID, threadID).getBody();
        }

        public static List<String> GetThreadDetails(int forumID, int subForumID, int threadID)
        {
            AppDomain.CurrentDomain.SetData("SQLServerCompactEditionUnderWebHosting", true);
            LogicForumImp forumManager = new LogicForumImp();
            Thread thread = forumManager.GetWebThread(forumID, subForumID, threadID);
            List<String> threadDetails = new List<String>();
            threadDetails.Add(thread.getTitle());
            threadDetails.Add(thread.getBody());
            threadDetails.Add(thread.getUser().getUserName());
            threadDetails.Add(thread.getTime().ToString());
            threadDetails.Add(forumManager.getForum(null, forumID).getTitle());
            threadDetails.Add(forumManager.getSubForum(null, forumID, subForumID).getTitle());
            return threadDetails;
        }
    }
}